package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public List<ProductModel> getProduceModelList(ProductModel productModel) {
        String sql = "select * from (\n" +
                "select p.*,c.name cuisineName from product p\n" +
//                以前后台管理，必须查询出所有产品，无论是否关联菜系
//                现在前端查询产品就必须关联菜系
                "inner join cuisine c\n" +
                "on p.cuisine_id=c.id )\n" +
                " temp where 1=1\n";
        if (productModel.getWhereShow() != -1) {
            sql += " and where_show=" + productModel.getWhereShow();
        }
        if (productModel.getCuisineId() != -1) {
            sql += " and cuisine_id=" + productModel.getCuisineId();
        }
        sql += " order by id desc ";

        sql += " limit " + (productModel.getPage() - 1) * productModel.getLimit() + "," + productModel.getLimit();

        ResultSet resultSet = JDBC.getJdbc().query(sql);
        List<ProductModel> list = new ArrayList<>();

        try {
            while (resultSet.next()) {
                ProductModel model = new ProductModel();
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setNormalPrice(resultSet.getBigDecimal("normal_price"));
                model.setPrice(resultSet.getBigDecimal("price"));
                model.setImgHref(resultSet.getString("img_href"));
                model.setCuisineName(resultSet.getString("cuisineName"));
                model.setContent(resultSet.getString("content"));
                model.setCuisineId(resultSet.getInt("cuisine_id"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<ProductModel> getProduceDetail(ProductModel productModel) {
        String sql = "select  * from product where id=(select min(id) from product where id>" + productModel.getId() + ")\n" +
                "union all\n" +
                "select * from product where id=" + productModel.getId() + "\n" +
                "union all\n" +
                "select  * from product where id=(select max(id) from product where id<" + productModel.getId() + ")";
        ResultSet resultSet = JDBC.getJdbc().query(sql);
        List<ProductModel> list = new ArrayList<>();

        try {
            while (resultSet.next()) {
                ProductModel model = new ProductModel();
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setNormalPrice(resultSet.getBigDecimal("normal_price"));
                model.setPrice(resultSet.getBigDecimal("price"));
                model.setImgHref(resultSet.getString("img_href"));
//                model.setCuisineName(resultSet.getString("cuisineName"));
                model.setContent(resultSet.getString("content"));
                model.setCuisineId(resultSet.getInt("cuisine_id"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
